package com.xxl.service.impl;

import com.xxl.dao.follow.FollowBlogMapper;
import com.xxl.dao.user.UserMapper;
import com.xxl.dto.FollowBlogDto;
import com.xxl.dto.UserDto;
import com.xxl.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xxl
 * @date 2023/3/30
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    FollowBlogMapper followBlogMapper;

    @Override
    public UserDto login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    public String findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public List<String> queryAllFollow(String username) {
        ArrayList<String> allFollowsName = new ArrayList<>();
        //返回所有关注的名字
        List<FollowBlogDto> followBlogDtoList = followBlogMapper.queryAllFollowsOrFollowings(username, null);
        //如果为空表示此用户没有关注任何人
        if (followBlogDtoList != null) {
            followBlogDtoList.forEach(t -> {
                allFollowsName.add(t.getFollowUsername());
            });
        }
        return allFollowsName;
    }

    @Override
    public List<String> queryAllFollowing(String username) {
        ArrayList<String> allFollowingsName = new ArrayList<>();
        //返回所有粉丝的名字
        List<FollowBlogDto> followBlogDtoList = followBlogMapper.queryAllFollowsOrFollowings(null, username);
        //如果为空表示此用户没有任何粉丝
        if (followBlogDtoList != null) {
            followBlogDtoList.forEach(t -> {
                allFollowingsName.add(t.getFollowingUsername());
            });
        }
        return allFollowingsName;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public  int register(UserDto  userDto) {
        return userMapper.addUser(userDto);
    }



    @Override
    @Transactional(rollbackFor = SQLException.class)
    public <T> int modifyUser(T t, String username) {
        return userMapper.modifyUser(username, (UserDto) t);
    }

    @Override
    public List<UserDto> findSomeOne(String name) {
        return userMapper.queryUserByUsername(name);
    }

    @Override
    public List<UserDto> queryAllUser() {
        return userMapper.queryAllUser();
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int deleteUserByUsername(String username) {
        return userMapper.deleteUserByUsername(username);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int followUser(String followName, String followingName) {
        assert followName != null && followingName != null : "不能为空";
        //自己不能关注自己
        if(followingName.equals(followName)) {
            log.error("自己不可以关注自己");
            return 0;
        }

        //查找followingName(粉丝的所有关注)
        List<String> list = queryAllFollow(followingName);
        //如果不为空表示当前用户是有关注的
        if (list != null) {
            for (String s : list) {
                //如果关注列表中有本次想关注的名字，就不需要关注了
                if (s.equals(followName)) {
                    log.error("您已经关注了此用户");
                    return 0;
                }
            }
        }
        return followBlogMapper.followUser(followingName, followName);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int cancelFollowUser(String followName, String followingName) {
        assert followName != null && followingName != null : "不能为空";
        //自己不能取消关注自己
        if(followingName.equals(followName)) {
            log.error("自己不可以取消关注自己");
            return 0;
        }
        //查找followingName(粉丝的所有关注)
        List<String> list = queryAllFollow(followingName);
        //如果不为空表示当前用户是有关注的
        if (list != null) {
            for (String s : list) {
                //如果关注列表中有本次想取消关注的名字，就取消关注
                if (s.equals(followName)) {
                    return followBlogMapper.canselFollow(followingName, followName);
                }
            }
        }
        log.error("您没有关注此用户");
        return 0;
    }
}
